package com.example.addressbook;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; 
import android.content.Intent; 
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.LayoutInflater;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v7.widget.RecyclerView;
import android.content.Context;
import com.bumptech.glide.Glide;
import org.litepal.crud.DataSupport;
import android.widget.PopupMenu;
import android.app.Dialog;
import android.view.MenuItem;
import android.view.Gravity; 
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.net.Uri;
import android.view.ContextMenu;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.support.v4.content.ContextCompat;
import android.content.pm.PackageManager; 
import android.Manifest;
import android.support.v4.app.ActivityCompat;
import android.app.AlertDialog;
import android.os.Build; 
import android.support.v4.content.FileProvider;  
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.support.v7.widget.LinearLayoutManager;
  
import java.util.*;
import java.io.*;

public class PeopleAdapter extends RecyclerView.Adapter<PeopleAdapter.ViewHolder>{
	
	private Context context;
	private List<People> peoples = new ArrayList<>();

	public PeopleAdapter(Context c){
		context = c;		// getActivity()
		getDataFromDB();
	}
	static class ViewHolder extends RecyclerView.ViewHolder{
		ImageView imageView;
		TextView textView;
		ImageView menu;
		
		public ViewHolder(View v){
			super(v);
			imageView = (ImageView)v.findViewById(R.id.circle_image_view);
			textView = (TextView)v.findViewById(R.id.text_view);
			menu = (ImageView)v.findViewById(R.id.menu_image_view);
		}
	}
	
	private Context mContext;
	
	@Override
	public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
		mContext = parent.getContext();		
		View view = LayoutInflater.from(parent.getContext())
			.inflate(R.layout.recycler_view_2_item,parent,false);
			
			final ViewHolder holder = new ViewHolder(view);
			holder.imageView.setOnClickListener( new View.OnClickListener(){
				@Override
				public void onClick(View v){
					int position = holder.getAdapterPosition();
					
					/*AlertDialog dialog = createDialog(holder.imageView);
					dialog.show();
					
					int id = SelectAdapter.getPictureId();
					Glide.with(context).load(id).into(holder.imageView);
					People p = peoples.get(position);
					People newP = new People(p.getName(),p.getTelephone(),p.getInformation(),id);
					deleteFromDB(position);
					newP.save();
					p.setPicturePath(id);
					//DataSupport.updateAll("name = ?",p.getName());*/
					//Toast.makeText(v.getContext(),"you click view:"+position,Toast.LENGTH_SHORT).show();
					Toast.makeText(v.getContext(),"Don't touch me ~~ "
						,Toast.LENGTH_SHORT).show();

				}
			});
			view.setOnClickListener(new View.OnClickListener(){
				@Override
				public void onClick(View v){
					Intent intent = new Intent(context,ItemDetailActivity.class);	//go to detail
					int position = holder.getAdapterPosition();
					People p = peoples.get(position);
					intent.putExtra("tag","people");
					intent.putExtra("people_data",p);
					((MainActivity)context).startActivityForResult(intent,2);
				}
			});
			holder.menu.setOnClickListener(new View.OnClickListener(){
					@Override
					public void onClick(View v){
						final int position = holder.getAdapterPosition();		
						final People p = peoples.get(position);
						
						PopupMenu pm = new PopupMenu(context,v);
						pm.getMenuInflater().inflate(R.menu.popup_menu,pm.getMenu());
						pm.show();
						pm.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener(){
							@Override
							public boolean onMenuItemClick(MenuItem item){
								switch(item.getItemId()){
									case R.id.call_item:
										String telNumber = p.getTelephone();
										Intent i = new Intent(Intent.ACTION_CALL);
										i.setData(Uri.parse("tel:"+telNumber));
										((MainActivity)context).startActivityForResult(i,3);
									case R.id.friend_item:
										List<Friend> list = DataSupport.where("name = ?",p.getName()).find(Friend.class);
										if( list.size() == 0 ){
											Friend f = new Friend(p.getName(),p.getTelephone(),
												p.getInformation(),p.getPicturePath());
											f.save();
											((MainActivity)context).fragmentAdapter.notifyDataSetChanged();
										}
										break;
									case R.id.detail_item:
										Intent intent = new Intent(context,ItemDetailActivity.class);
										intent.putExtra("tag","people");
										intent.putExtra("people_data",peoples.get(position));
										((MainActivity)context).startActivityForResult(intent,2);
										break;
									case R.id.modify_item:
										Intent  inte = new Intent(context,ModifyActivity.class);
										inte.putExtra("name",p.getName());
										((MainActivity)context).startActivityForResult(inte,1);
										break;
									case R.id.delete_item:
										delete(position);
										break;
								}
								return true;
							}
						});
					}
			});
			return holder;
	}
	
	@Override
	public void onBindViewHolder(ViewHolder holder,int position){
		People p = peoples.get(position);
		int i = Integer.valueOf(p.getPicturePath()).intValue();
		Glide.with(mContext).load(i).into(holder.imageView);
		holder.textView.setText(p.getName());
	}
	
	@Override
	public int getItemCount(){
		return peoples.size();
	}
	
	public void delete(int position){
		DataSupport.deleteAll(People.class,"name = ?",peoples.get(position).getName());		//database
		peoples.remove(position);
		notifyItemRemoved(position);
		notifyItemRangeChanged(position,peoples.size()-position);
	}
	
	public void deleteFromDB(int position){
		DataSupport.deleteAll(People.class,"name = ?",peoples.get(position).getName());		//database
	}
	
	public void getDataFromDB(){
		peoples = DataSupport.findAll(People.class);	//database
	}
	
	public AlertDialog createDialog(ImageView imageView){
		AlertDialog.Builder dialog = new AlertDialog.Builder(context);
		View view = LayoutInflater.from(context).inflate(R.layout.alert_dialog,null);
		dialog.setView(view);

		AlertDialog d = dialog.create(); 
		RecyclerView recyclerView = view.findViewById(R.id.select_recycler_view);
		SelectAdapter selectAdapter = new SelectAdapter(context,d,imageView);
		LinearLayoutManager layoutManager = new LinearLayoutManager(context);
		layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
		recyclerView.setLayoutManager(layoutManager);
		recyclerView.setAdapter(selectAdapter);
		
		return d;
	}
}